<?
/**
 * CoreValidation 
 * 验证类,主要对变量进行验证
 * @package Validation.class.php
 * @version $id$
 * @copyright blankyao
 * @author blankyao <blankyao@gmail.com> 
 * @license GNU General Public License 2.0 
 * @todo All
 */
include_once("Exception.class.php");
class Core_Validation
{
    /**
     * obj 
     * 要检查或过滤的对象
     * @var mixed
     * @access public
     */
    var $obj;

    /**
     * Core_Validation 
     * 构造函数,初始化需要检查的对象
     * @param mixed $_obj 
     * @access public
     * @return void
     */
    function Core_Validation($_obj) {
        $this->obj = $_obj;
    }
	/**
	 * 检测一个用户名的合法性
	 * 
	 * @param int $chkType 要求用户名的类型
     * 1为英文、数字、下划线，2为任意可见字符，3为中文(GBK)、
     * 英文、数字、下划线，4为中文(UTF8)、英文、数字，缺省为1
	 * @return bool 返回检查结果，合法为true，非法为false
	 */

	function chkUserName($chkType=1){
		switch($chkType){
			case 1:
				$result = preg_match("/^[a-zA-Z0-9_]+$/i", $this->obj);
				break;
			case 2:
				$result = preg_match("/^[\w\d]+$/i", $this->obj);
				break;
			case 3:
				$result = preg_match("/^[_a-zA-Z0-9\0x80-\0xff]+$/i", $this->obj);
				break;
			case 4:
				$result = preg_match("/^[_a-zA-Z0-9\u4e00-\u9fa5]+$/i", $this->obj);
				break;
			default:
				$result = preg_match("/^[a-zA-Z0-9_]+$/i", $this->obj);
				break;
		}
		return $result;
	}
	

	/**
	 * email地址合法性检测
	 */
	function isEmail(){
		return preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $this->obj);
	}

	/**
	 * URL地址合法性检测
	 */
	function isUrl(){
		return preg_match("/^http:\/\/[\w]+\.[\w]+[\S]*/", $this->obj);
	}

	/**
	 * 是否是一个合法域名
	 */
	function isDomainName(){
		return preg_match("/^[a-z0-9]([a-z0-9-]+\.){1,4}[a-z]{2,5}$/i", $this->obj);
	}

	/**
	 * 检测IP地址是否合法
	 */
	function isIpAddr(){
		return preg_match("/^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/", $this->obj);
	}

	/**
	 * 邮编合法性检测
	 */
	function isPostalCode(){
		return (is_numeric($this->obj) && (strlen($this->obj)==6));
	}

	/**
	 * 电话(传真)号码合法性检测
	 */
	function isPhone(){
		return preg_match("/^(\d){2,4}[\-]?(\d+){6,9}$/", $this->obj);
	}

	/**
	 * 手机号码合法性检查
	 */
	 function isMobile(){
		return preg_match("/^(13|15)\d{9}$/i", $this->obj);
	 }

	/**
	 * 身份证号码合法性检测
	 */
	function isIdCard(){
		return preg_match("/^(\d{15}|\d{17}[\dx])$/i", $this->obj);
	}

	/**
	* 严格的身份证号码合法性检测(按照身份证生成算法进行检查)
	*/
	function chkIdCard(){
		if (strlen($this->obj) != 18){
			return false;
		}
		$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
		$ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 
		$this->obj = strtoupper($this->obj);
		$sigma = '';
		for ($i = 0; $i < 17; $i++) {
			$sigma += ((int) $this->obj{$i}) * $wi[$i]; 
		} 
		$parity_bit = $ai[($sigma % 11)];
		if ($parity_bit != substr($this->obj, -1)){
			return false;
		}
		return true;
	}

	/**
	 * 检测是否包含特殊字符
	 */
	function chkSpecialWord(){
		return preg_match('/>|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i', $this->obj);
	}

	/**
	 * 过滤特殊字符
	 */
	function filterSpecialWord(){
		return preg_replace('/>|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i', "", $this->obj);
	}

	/**
	 * 过滤SQL注入攻击字符
	 */
	function filterSqlInject(){
		if (!get_magic_quotes_gpc()){
			return addslashes($this->obj);
		}
		return $this->obj;		
	}

	/**
	 * 过滤HTML标签
	 *
	 * @param bool $strict - 是否严格过滤（严格过滤将把所有已知HTML标签开头的内容过滤掉）
	 * @return string 返回替换后的结果
	 */
	function stripHtmlTag($strict=false){
		$this->obj = strip_tags($this->obj);
		if (!$strict){
			return $this->obj;
		}
		$html_tag = "/<[\/|!]?(html|head|body|div|span|DOCTYPE|title|link|meta|style|p|h1|h2|h3|h4|h5|h6|strong|em|abbr|acronym|address|bdo|blockquote|cite|q|code|ins|del|dfn|kbd|pre|samp|var|br|a|base|img|area|map|object|param|ul|ol|li|dl|dt|dd|table|tr|td|th|tbody|thead|tfoot|col|colgroup|caption|form|input|textarea|select|option|optgroup|button|label|fieldset|legend|script|noscript|b|i|tt|sub|sup|big|small|hr)[^>]*>/is";
		return preg_replace($html_tag, "", $this->obj);
	}

	/**
	 * 转换HTML的专有字符
	 */
	 function filterHtmlWord(){
		if (function_exists('htmlspecialchars')){
			return htmlspecialchars($this->obj);
		}
		$search = array("&", '"', "'", "<", ">");
		$replace = array("&amp;", "&quot;", "&#039;", "&lt;", "&gt;");
		return str_replace($search, $replace, $this->obj);
	 }

	 /**
	  * 剔除JavaScript、CSS、Object、Iframe
	  */
	 function filterScript(){
		$this->obj = preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i","&111n\\2",$this->obj);
		$this->obj = preg_replace ("/<style.+<\/style>/iesU", '', $this->obj);
		$this->obj = preg_replace ("/<script.+<\/script>/iesU", '', $this->obj);
		$this->obj = preg_replace ("/<iframe.+<\/iframe>/iesU", '', $this->obj);
		$this->obj = preg_replace ("/<object.+<\/object>/iesU", '', $this->obj);
		return $this->obj;
	 }

	/**
	 * 过滤JAVASCRIPT不安全情况
	 */
	function escapeScript(){
		$this->obj = preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i","&111n\\2",$this->obj);
		$this->obj = preg_replace("/<script(.*?)>(.*?)<\/script>/si","",$this->obj);
		$this->obj = preg_replace("/<iframe(.*?)>(.*?)<\/iframe>/si","",$this->obj);
		$this->obj = preg_replace ("/<object.+<\/object>/iesU", '', $this->obj);
		return $this->obj;
	}



}

?>
